from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, redirect
from relationships.models import RelationshipStatus
from django.http import HttpResponse
"""
def require_user(view):
    def inner(request, username, *args, **kwargs):
        user = get_object_or_404(User, username=username)
        return view(request, user, *args, **kwargs)
    return inner
"""

def require_owner(view):
    def require_owner(request, username, *args, **kwargs):

        user = get_object_or_404(User, username=username)
        
        if user:
            user_owner = RelationshipStatus.objects.filter(owner__id=user.id, id=kwargs['pk'])

            if user_owner:
                return view(request, user_owner, *args, **kwargs)
            else:
                return redirect('relationship_list_base')
        else:
            return view(request, user, *args, **kwargs)

    return require_owner

def require_owner_list(view):
    def require_owner_list(request, username, *args, **kwargs):
        
        user = get_object_or_404(User, username=username)
        
        if user:
            users = user.username == request.user.username
            if users == True:
                return view(request, user, *args, **kwargs)
            else:
                return redirect('relationship_list_base')
        else:
            return view(request, user, *args, **kwargs)
    
    return require_owner_list